package com.htfyun.fragment.postil;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.htfyun.utils.DebugLog;
import com.yinghe.whiteboardlib.Utils.BitmapUtils;

import org.geometerplus.zlibrary.ui.android.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sst on 2017/12/6.
 */

public class HtfyunPostilAdapter extends RecyclerView.Adapter<HtfyunPostilAdapter.MyViewHolder> {

    private LayoutInflater mInflater;
    private Context mContext;

    private List<String> pngPathList = new ArrayList<>();

    private Rect bgRect = new Rect();
    private Paint bgPaint = new Paint();

    private void initPaint() {
        
        bgPaint.setAntiAlias(true);
        bgPaint.setDither(true);
        bgPaint.setColor(Color.BLACK);
        bgPaint.setStrokeWidth(4);
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setStrokeJoin(Paint.Join.MITER);
        bgPaint.setStrokeCap(Paint.Cap.BUTT);

    }

    public HtfyunPostilAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mContext = context;

        initPaint();
    }

    public void setDatas(List<String> items) {
        pngPathList.clear();
        pngPathList.addAll(items);
        
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.htfyun_postil_adapter_item, parent, false);
        final MyViewHolder holder = new MyViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                if (pngPathList.size() > position) {
                    String path = pngPathList.get(position);
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName("com.htfyun.funtest",
                            "android.penwrite.test.EinkDrawActivity "));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("path", path);
                    
                    mContext.startActivity(intent);
                }
                
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        if (pngPathList.size() <= position) {
            return;
        }
        String path = pngPathList.get(position);

        Bitmap myBitmap = getSDCardPhoto(path);
        if (myBitmap != null) {
            Bitmap bitmapNew = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), myBitmap.getConfig());
            Canvas canvas = new Canvas(bitmapNew);
            bgRect.set(0, 0, myBitmap.getWidth(), myBitmap.getHeight());
            canvas.drawBitmap(myBitmap, new Matrix(), null);
            canvas.drawRect(bgRect, bgPaint);
            canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.restore();

            holder.img.setImageBitmap(bitmapNew);
            
        }
        
    }
    
    
    

    @Override
    public int getItemCount() {
        return pngPathList.size();
    }


    private Bitmap getSDCardPhoto(String path) {
        File file = new File(path);
        DebugLog.e("photo path = " + path);
        try {
            if (file.exists()) {
                return BitmapUtils.decodeSampleBitMapFromFile(mContext, path, 0.35f);
            }
        } catch (Exception e) {
            
        }

        return null;

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;

        public MyViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.img);
        }
    }
}
